import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tplib/utils/res_utils.dart';

class CommTextField extends StatefulWidget{

  final TextInputAction textInputAction;
  final Color foucosUnderlineColor;
  final Color defaultUnderlineColor;
  final TextStyle? textStyle;
  final TextAlign? textAlign;
  final TextStyle? hintTextStyle;
  final String? hintText;
  final TextStyle? lableTextStyle;
  final String? lableText;
  final int maxLength;
  final int maxLines;
  final double? paddingBottm;
  final bool isInputPwd;
  final bool autoFocus;
  final bool isShowDeleteMain;
  final bool enabled;
  final bool? readOnly;
  final TextInputType? keyboardType;
  final List<TextInputFormatter>? inputFormatters;
  final Function? onSubmitted;
  final Function? onChanged;
  final String? defaultCont;

  CommTextField({
    Key? key,
    this.textInputAction:TextInputAction.done,
    this.foucosUnderlineColor:const Color(0xFF0E95FD),
    this.defaultUnderlineColor:const Color(0xFFcccccc),
    this.textStyle,
    this.textAlign:TextAlign.start,
    this.hintTextStyle,
    this.hintText,
    this.lableTextStyle,
    this.lableText,
    this.maxLength: 20,
    this.maxLines: 1,
    this.paddingBottm,
    this.isInputPwd: false,
    this.autoFocus: false,
    this.isShowDeleteMain: false,
    this.enabled: true,
    this.readOnly: false,
    this.keyboardType: TextInputType.text,
    this.inputFormatters,
    this.onSubmitted,
    this.onChanged,
    this.defaultCont,
  }):super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _CommTextFieldState();
  }

}

class _CommTextFieldState extends State<CommTextField> {
  bool _isShowDelete = false;
  TextEditingController? _editingController;
  @override
  void initState() {
    super.initState();
    _editingController=TextEditingController(text: widget.defaultCont);
    //监听输入改变
    _editingController!.addListener((){
      print('是否显示删除按钮=$_isShowDelete');
      if(mounted){ //mounted 挂在后在setState
        setState(() {
          _isShowDelete = _editingController!.text.isNotEmpty;
          print('是否显示删除按钮 2=$_isShowDelete');
        });
      }

    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Stack(
      alignment: Alignment.centerRight,
      children: <Widget>[
        TextField(
          style: widget.textStyle,
          enabled:widget.enabled ,
          textAlign: widget.textAlign??TextAlign.start,
          maxLength: widget.maxLength,
          maxLines: widget.maxLines,
          obscureText: widget.isInputPwd,
          autofocus: widget.autoFocus,
          controller: _editingController,
          textInputAction: widget.textInputAction,
          keyboardType: widget.keyboardType??TextInputType.text,
          readOnly: widget.readOnly??false,
          onSubmitted: (val){
//            print("点击了键盘上的动作按钮，当前输入框的值为：${val}");
            if(widget.onSubmitted!=null){
              widget.onSubmitted!(val);
            }
          },
          onChanged: (value){
            if(widget.onChanged!=null)widget.onChanged!(value);
          },
          cursorColor: ColorUtils.color_dcbe9b,
//          // 数字、手机号限制格式为0到9(白名单)， 密码限制不包含汉字（黑名单）
          inputFormatters:widget.inputFormatters,
//          inputFormatters: (widget.keyboardType == TextInputType.number || widget.keyboardType == TextInputType.phone) ?
//          [WhitelistingTextInputFormatter(RegExp("[0-9]"))] : [BlacklistingTextInputFormatter(RegExp("[\u4e00-\u9fa5]"))],
          decoration: InputDecoration(
              contentPadding: EdgeInsets.only(
                bottom: widget.paddingBottm??PxUtils.px_4,
              ),
              isDense: true,
              hintText: widget.hintText,
              hintStyle: widget.hintTextStyle??TextStyle(fontSize: PxUtils.px_12,color: ColorUtils.color_999999),
              labelText: widget.lableText,
              labelStyle: widget.lableTextStyle,
              counterText: "",
              focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(
                      color: widget.foucosUnderlineColor,
                      width: 1
                  )
              ),
              enabledBorder: UnderlineInputBorder(
                  borderSide: BorderSide(
                      color: widget.defaultUnderlineColor,
                      width: 1
                  )
              )
          ),
        ),
        //删除按钮
        Visibility(
          visible: widget.isShowDeleteMain==true?_isShowDelete:false,
          child: InkWell(
            highlightColor: Colors.white,
            splashColor: Colors.white,
            child: Icon(
                Icons.cancel,
              size: 14,
              color: ColorUtils.color_999999,
            )
            // ImageUtils.loadAssetImage(DrawableUtils.editcloseimg,
            //   width: 14.0,
            //   height: 14.0,
            // )
            ,
            onTap: (){
              if(mounted){ //mounted 挂在后在setState
                setState(() {
                  _editingController!.text = "";
                });
                if(widget.onChanged!=null)widget.onChanged!('');
              }
            },
          ),
        ),
      ],
    );
  }
}